package cn.com.majiang.service.impl;

import cn.com.majiang.dao.NoticetableDao;
import cn.com.majiang.domain.Noticetable;
import cn.com.majiang.param.NoticetableParam;
import cn.com.majiang.service.INoticetableService;
import cn.com.majiang.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jiangyanxin on 2017/5/13.
 */
@Service
public class NoticetableServiceImpl implements INoticetableService{
    @Autowired
    private NoticetableDao noticetableDao;
    @Override
    public Boolean addNoticetable(Noticetable noticetable) {
        return noticetableDao.insertNoticetable(noticetable) > 0;
    }

    @Override
    public Map<String, Object> searchNoticetableList(NoticetableParam param) {
        List<Map<String,String>> resultList = new ArrayList<>();
        int count = noticetableDao.selectNoticetableCount(param);
        if(param.getPage() != null) {
            param.setStartNum(param.getLimit() * (param.getPage() - 1));
        }
        List<Noticetable> list = noticetableDao.selectNoticetableList(param);
        if(list != null && list.size()>0){
            for(Noticetable noticetable : list){
                Map<String,String> map = converter(noticetable);
                resultList.add(map);
            }
        }
        Map map = new HashMap();
        map.put("noticetableList",resultList);
        map.put("count",count);
        return map;
    }

    private Map<String,String> converter(Noticetable noticetable){
        Map<String,String> map = new HashMap();
        map.put("title",noticetable.getTitle());
        map.put("type",noticetable.getType()+"");
        map.put("createTime",DateUtils.dateTimeToStr(noticetable.getCreateTime()));
        map.put("startTime", DateUtils.dateTimeToStr(noticetable.getStartTime()));
        map.put("endTime", DateUtils.dateTimeToStr(noticetable.getEndTime()));
        map.put("content",noticetable.getContent());
        map.put("id",noticetable.getId()+"");
        return map;

    }

    @Override
    public Boolean editNoticetable(Noticetable noticetable) {
        return noticetableDao.updateByPrimaryKeySelective(noticetable)>0;
    }

    @Override
    public Map<String,String> getNoticetableById(Noticetable noticetable) {
        Noticetable result = noticetableDao.selectNoticetableById(noticetable);
        return converter(result);
    }
}
